<template>
  <div v-if="routeName === 'Role'" class="list-page">
    <div class="tool-btns">
      <el-button
        type="primary"
        size="small"
        @click="$router.push({ name: 'AddRole' })"
        >创建角色</el-button
      >
    </div>
    <div class="block-wrap filter-form">
      <el-form :inline="true" :model="form" size="small" ref="ruleForm">
        <el-form-item label="角色名称">
          <el-input
            v-model="form.roleName"
            placeholder="可按角色名称模糊查询"
          />
        </el-form-item>
        <el-form-item class="filter-form-btns">
          <el-button @click="search" type="primary">查询</el-button>
          <el-button type="default" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="block-wrap">
      <el-table
        :data="dataList"
        style="width: 100%;"
        align="center"
        header-align="center"
        :header-cell-style="headerStyle"
        :cell-style="cellStyle"
        v-loading="loading"
      >
        <el-table-column
          type="index"
          label="序号"
          width="60"
          :index="getIndex"
        />
        <el-table-column prop="roleName" label="角色名" />
        <el-table-column prop="usernames" label="关联管理员" />
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            {{ getCreateTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="options" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="deleteBtn"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="form.limit"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, prev, pager, next, sizes, jumper"
        :total="total"
      />
    </div>
    <div class="footer">
      Copyright © 2018-2020 湖南智慧政务区块链科技有限公司
    </div>
  </div>
  <router-view v-else :key="key" />
</template>

<script>
import { getRoleListBypage, deleteRole } from "@/api/sys";
import { parseTime } from "@/utils/index";
export default {
  name: "Role",
  computed: {
    key() {
      return this.$route.path;
    },
    routeName() {
      return this.$route.name;
    }
  },
  data() {
    return {
      form: {
        roleName: "",
        page: 1,
        limit: 10
      },
      dataList: [],
      total: 0,
      loading: false
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    getRoleList() {
      this.loading = true;
      getRoleListBypage(this.form)
        .then(res => {
          this.total = res.data.total;
          this.dataList = res.data.records;
        })
        .catch(() => {
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    search() {
      this.getRoleList();
    },
    handleSizeChange(val) {
      this.form.limit = val;
      this.getRoleList();
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getRoleList();
    },
    getCreateTime(time) {
      let createTime = new Date(time).getTime();
      return parseTime(createTime);
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将删除该角色, 是否继续", "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning"
      })
        .then(() => {
          deleteRole(row.roleId).then(res => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getRoleList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 编辑
    handleEdit(row) {
      this.$router.push({
        name: "AddRole",
        query: { roleId: row.roleId }
      });
    },
    //重置
    resetForm() {
      this.form.roleName = "";
      this.getRoleList();
    },
    headerStyle() {
      return {
        background: "#F5F9FF",
        color: "#2E2E30",
        textAlign: "center"
      };
    },
    cellStyle() {
      return {
        height: "48px",
        padding: "0",
        textAlign: "center"
      };
    },

    getIndex($index) {
      return $index + (this.form.page - 1) * this.form.limit + 1;
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/page/list-page.scss";
.list-page {
  .block-wrap {
    .el-pagination {
      ::v-deep.el-pagination__total {
        float: left;
      }
    }
  }
}
.block-wrap {
  padding: 25px;
  background: #fff;
  border-radius: 2px;
  margin-bottom: 16px;
}
.el-form-item--small .el-form-item {
  margin-right: 60px;
}
::v-deep.list-page .filter-form .filter-form-btns {
  margin-right: 10px;
}
.el-table .el-table__cell {
  text-align: center;
}
.el-button--text {
  color: #5892ff;
}
.el-button--text.deleteBtn {
  color: #ff3535;
}
::v-deep.el-pagination .btn-prev,
::v-deep.el-pagination .btn-next,
::v-deep.el-pagination .el-pager li {
  border: 1px solid #eee;
  margin-right: 8px;
}
::v-deep.el-pagination .el-pager {
  li.active + li {
    border-left: 1px solid #eee !important;
  }
}
::v-deep.el-pagination .el-pager {
  li.active {
    border-color: #295ec8 !important;
  }
}
.footer {
  text-align: center;
  margin-top: 50px;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.45);
  line-height: 20px;
}
</style>
